﻿using _2308NA.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using _2308NA.Model;
using Newtonsoft.Json;
using System.Web.SessionState;
using System.Security.Policy;

namespace _2308NA.UI.Ajax
{
    /// <summary>
    /// UsersHandler 的摘要说明
    /// </summary>
    public class UsersHandler : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";

            string result = "";
            string type = context.Request.QueryString["type"];
            switch (type)
            {
                case "list":
                    result = Select(context);
                    break;
                case "add":
                    result = Add(context);
                    break;
                case "info":
                    result = GetInfo(context);
                    break;
                case "update":
                    result = Update(context);
                    break;
                case "delete":
                    result = Delete(context);
                    break;
                case "delbatch":
                    result = DeleteBatch(context);
                    break;

                case "login":
                    result = Login(context);
                    break;
                case "user":
                    result = GetUser(context);
                    break;
                case "out":
                    result = Out(context);
                    break;

                default:
                    break;
            }


            context.Response.Write(result);
            context.Response.End();
        }

        //增加
        private static string Add(HttpContext context)
        {
            string model = context.Request.Form["model"];
            Users users = JsonConvert.DeserializeObject<Users>(model);
            int num = new UsersBll().AddUser(users);

            Result result = new Result();
            result.code = num > 0 ? 1 : 0;
            result.msg = num > 0 ? "ok!" : "error!";
            return JsonConvert.SerializeObject(result);
        }

        //删除
        private static string Delete(HttpContext context)
        {
            string id = context.Request.Form["id"];
            UsersBll bll = new UsersBll();
            int num = bll.DeleteUser(int.Parse(id));

            Result result = new Result();
            result.code = num > 0 ? 1 : 0;
            result.msg = num > 0 ? "ok!" : "error!";
            return JsonConvert.SerializeObject(result);
        }

        //批量删除
        private static string DeleteBatch(HttpContext context)
        {
            string ids = context.Request.Form["ids"];
            UsersBll bll = new UsersBll();
            int num = bll.DeleteBatch(ids);

            Result result = new Result();
            result.code = num > 0 ? 1 : 0;
            result.msg = num > 0 ? "ok!" : "error!";
            return JsonConvert.SerializeObject(result);
        }

        //修改
        private static string Update(HttpContext context)
        {
            string model = context.Request.Form["model"];
            Users users = JsonConvert.DeserializeObject<Users>(model);
            int num = new UsersBll().UpdateUser(users);

            Result result = new Result();
            result.code = num > 0 ? 1 : 0;
            result.msg = num > 0 ? "ok!" : "error!";
            return JsonConvert.SerializeObject(result);
        }

        //单条数据查询
        private static string GetInfo(HttpContext context)
        {
            string id = context.Request.QueryString["id"];
            UsersBll bll = new UsersBll();
            Users user = bll.GetUsers(int.Parse(id));

            ResultData<Users> result = new ResultData<Users>();
            result.code = user != null ? 1 : 0;
            result.msg = user != null ? "ok!" : "null!";
            result.data = user;
            return JsonConvert.SerializeObject(result);
        }
        //多条数据查询
        private static string Select(HttpContext context)
        {
            string name = context.Request.QueryString["name"];
            string page = context.Request.QueryString["page"];
            string size = context.Request.QueryString["size"];
            int count = 0;
            UsersBll bll = new UsersBll();
            List<Users> list = bll.GetUsers(name, int.Parse(page), int.Parse(size), out count);

            ResultPageList<Users> result = new ResultPageList<Users>();
            result.code = count > 0 ? 1 : 0;
            result.msg = count > 0 ? "ok!" : "null!";
            result.count = count;
            result.data = list;
            return JsonConvert.SerializeObject(result);
        }

        //登录
        private static string Login(HttpContext context)
        {
            //获取请求的手机号码和密码
            //输入  前端传递过来的
            string phone = context.Request.Form["phone"];
            string pwd = context.Request.Form["pwd"];

            UsersBll bll = new UsersBll();
            Users users = bll.Login(phone, pwd);

            //写 Session
            if (users!=null)
            {
                context.Session["uid"] = users.UserID;
            }
         

            Result result = new Result();
            result.code = users != null ? 1 : 0;
            result.msg = users != null ? "ok!" : "error!";
            return JsonConvert.SerializeObject(result);
        }



        //退出登录
        private static string Out(HttpContext context)
        {
            //清除所有Session
            context.Session.Clear();

            Result result = new Result();
            result.code = 1;
            result.msg = "ok!";
            return JsonConvert.SerializeObject(result);
        }

        /// <summary>
        /// 根据登录时存储的 Session["uid"] 获取个人信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private static string GetUser(HttpContext context)
        {
            Users user = null;
            if (context.Session["uid"]!=null)
            {
                int uid = int.Parse(context.Session["uid"].ToString());
                UsersBll bll = new UsersBll();
                user = bll.GetUsers(uid);
            }

            ResultData<Users> result = new ResultData<Users>();
            result.code = user != null ? 1 : 0;
            result.msg = user != null ? "ok!" : "null!";
            result.data = user;
            return JsonConvert.SerializeObject(result);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}